const mongoose = require('mongoose')
const Comment = mongoose.model('Comment')

// comment
exports.save = async (ctx, next) => {
  const _comment = ctx.request.body.comment

  if (_comment.cid) {
    let comment = await Comment.findOne({
      _id: _comment.cid
    })

    const reply = {
      from: _comment.from,
      to: _comment.tid,
      content: _comment.content
    }

    comment.reply.push(reply)
    await comment.save()

    ctx.body = {success: 1}
  } else {
    let comment = new Comment({
      movie: _comment.movie,
      from: _comment.from,
      content: _comment.content
    })

    await comment.save()
    ctx.body = {success: 1}
  }
}
